package com.junhong.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.junhong.entity.Student;
import com.junhong.service.StudentService;
import com.junhong.vo.VueData;
import com.opensymphony.xwork2.ActionSupport;

public class StudentListAction extends ActionSupport {
	private static final long serialVersionUID = 1L;
	private Integer pos;
	private Integer rows;

	@Resource
	private StudentService studentService;

	public void list() {
		HttpServletResponse response = ServletActionContext.getResponse();
		// 解决中文乱码问题
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter pw = null;
		try {
			pw = response.getWriter();
		} catch (IOException e) {
			e.printStackTrace();
		}
		// 判断参数是否为空
		if (pos == null) {
			pos = 0;
		}
		if (rows == null || rows == 0) {
			rows = 0;
		}
		List<Student> list = studentService.find(pos, rows);
		int total = (int) studentService.count();
		VueData<Student> vue = new VueData<Student>(list, total);
		String json_str = "";
		ObjectMapper mapper = new ObjectMapper();
		try {
			json_str = mapper.writeValueAsString(vue);
		} catch (JsonProcessingException e) {
			e.printStackTrace();
		}
		System.out.println(json_str);
		pw.write(json_str);
		pw.flush();
		pw.close();
	}

	public Integer getPos() {
		return pos;
	}

	public void setPos(Integer pos) {
		this.pos = pos;
	}

	public Integer getRows() {
		return rows;
	}

	public void setRows(Integer rows) {
		this.rows = rows;
	}

}
